<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Product;
use Illuminate\Support\Facades\Response;
use App\Models\Evaluate_log;
use App\Models\Region;

class MainController extends Controller
{
    public function __construct()
    {

        $this->middleware(function ($request, $next) {
            if (session('userid')) {
                $user = User::find(session('userid'));
            } else {
                $user = '';
            }
            $data['user'] = $user;
            view()->share($data);
            return $next($request);
        });
    }

    public function index()
    {
        $config = config('front');
        $indexProduct = Product::where('type', 'index')->get();
        return view('home.index', compact('config', 'indexProduct'));
    }

    public function productlist()
    {
        $config = config('front');
        $product = Product::where('type', 'list')->get();
        return view('home.productlist', compact('product', 'config'));
    }

    public function mortgage()
    {
        $config = config('front');
        return view('home.mortgage', compact('config'));
    }

    public function fund()
    {
        $config = config('front');
        return view('home.fund', compact('config'));
    }

    public function group()
    {
        $config = config('front');
        return view('home.group', compact('config'));
    }

    public function card()
    {
        $config = config('front');
        return view('home.card', compact('config'));
    }

    public function evaluate(Request $request)
    {
        if ($request->ajax()) {
            $data['evaluate'] = $request->get('evaluate');
            $data['job'] = $request->get('job');
            $data['house'] = $request->get('house');
            $data['car'] = $request->get('car');
            $data['mobile'] = $request->get('mobile');
            $imgcode = $request->get('imgcode');
            if (!captcha_check($imgcode)) {
                return Response::json(['code' => '0001', 'message' => '验证码错误']);
            }
            $data['create_time'] = date('Y-m-d H:i:s', time());
            $result = Evaluate_log::create($data);
            if ($result) {
                return Response::json(['code' => '0000', 'message' => '评估短信已发送成功，请注意查收']);
            } else {
                return Response::json(['code' => '0002', 'message' => '未知错误！']);
            }
        }
        $config = config('front');

        return view('home.evaluate', compact('config'));
    }

    public function aboutus()
    {
        return view('home.aboutus');
    }

    public function service()
    {
        return view('home.service');
    }

    public function newmanual(){
        return view('home.newmanual');
    }

    public function contactus(){
        return view('home.contactus');
    }
    public function serviceterms(){
        return view('home.serviceterms');
    }

    public function region(Request $request){
        $region_code=$request->get('code');
        $city=Region::where('p_code',$region_code)->get();
        $opt="<option>请选择城市</option>";
        foreach($city as $key=>$val){
            $opt.="<option value='{$val->region_code}'>".$val['region_name']."</option>";
        }
        return $opt;
    }
}
